package com.my.netty.promote.server.handler;

import com.my.netty.promote.server.session.SessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理客户端退出，包括正常退出和异常退出
 */
@Slf4j
@ChannelHandler.Sharable
public class QuitHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //连接断开时触发inactive事件，解绑channel
        SessionFactory.getSession().unbind(ctx.channel());
        log.info("{} 已断开", ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //出现异常时触发
        SessionFactory.getSession().unbind(ctx.channel());
        log.info("{} 已异常断开，异常是{}", ctx.channel(), cause.getMessage());
    }
}
